<?php

namespace App\Console\Commands\WorkOrder;

use App\Enums\WorkOrder\MissionEnum;
use App\Enums\WorkOrder\OrderEnum;
use App\Models\FuYou\WorkOrderFuyouInfoModel;
use App\Models\WorkOrder\WorkOrderApplyMainModel;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class InitFuyouStep extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'workOrder:initFuyouStep';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '工单-初始化富友流程步骤';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        WorkOrderApplyMainModel::query()->with(
            [
                'fuyouInfoRel' => function ($query) {
                    $query->orderByDesc('id');
                }
            ]
        )
            ->where('problemPid', OrderEnum::PROBLEM_PID_5)
            ->where('problemChildId', OrderEnum::PROBLEM_PID_5_CHILDREN_105)//夸父收银进件2.0
            ->where('status', OrderEnum::MAIN_STATUS_ING)                //1进行中
            ->whereIn('step', [MissionEnum::STEP_7, MissionEnum::STEP_8])//富友审核状态
            ->chunkById(
                100, function ($workOrderApplyMainModel) {
                foreach ($workOrderApplyMainModel as $workOrderApplyMain) {

                    $fuyouInfoId            = $workOrderApplyMain->fuyouInfoRel->id ?? 0;                     //id
                    $weChatAuthorizeStatus  = $workOrderApplyMain->fuyouInfoRel->weChatAuthorizeStatus ?? 0;  //微信授权状态 0待认证 1成功
                    $alipayAuthorizeStatus  = $workOrderApplyMain->fuyouInfoRel->alipayAuthorizeStatus ?? 0;  //支付宝授权状态 0待认证 1成功
                    $secondaryBindingStatus = $workOrderApplyMain->fuyouInfoRel->secondaryBindingStatus ?? 0; //富友一二级绑定邮件状态 0待认证 1成功
                    $preSystemApplyStatus   = $workOrderApplyMain->fuyouInfoRel->weChatAuthorizeStatus ?? 0;  //富友前置系统申请邮件状态 0待认证 1成功
                    if (!$fuyouInfoId) {
                        continue;
                    }
                    if ($weChatAuthorizeStatus && $alipayAuthorizeStatus) {
                        $step = WorkOrderFuyouInfoModel::DEFAULT_STEP_BUSINESS_AUTHENTICATION;
                        if ($secondaryBindingStatus && $preSystemApplyStatus) {
                            $step = WorkOrderFuyouInfoModel::DEFAULT_STEP_EMAIL_MCH_NAME_PASSWORD;
                        }
                    } else {
                        $step = WorkOrderFuyouInfoModel::DEFAULT_STEP_AUDIT;
                    }
                    if ($step) {
                        $data = [
                            'step' => $step,
                        ];
                        WorkOrderFuyouInfoModel::setInfoById($fuyouInfoId, $data);
                        Log::info(
                            'initFuyouStep', [
                            'fuyouInfoId' => $fuyouInfoId,
                            'data'        => $data,
                        ]
                        );
                    }

                }
            }
            );
    }
}
